import json
import os.path

class Book:
    def __init__(self, name, author, info, status=1):
        """
        书籍类
        :param name: 书名
        :param author: 作者
        :param info: 简介
        :param status: 借阅状态
        :return:
        """
        self.name = name
        self.author = author
        self.info = info
        self.status = status
    def __str__(self):
        # 书的信息格式化输出
        if self.status == 0:
            status = '已借阅'
        else:
            status = '未借阅'
        return '书名：{}\t 作者：{}\t 简介：{}\t 状态：{}\t'.format(self.name, self.author, self.info, status)
    def book_to_dict(self):
        return  {self.name:[self.author, self.info, self.status]}

class BookManager:
    def __init__(self):
        with open('./book_storage.json', 'a+') as f:
            f.seek(0)
            file = f.read()
            if len(file) == 0:
                self.books = {}
            else:
                f.seek(0)
                self.books = json.load(f)
    def menu(self):
        """
        用来处理交互逻辑
        :return:
        """
        while True:
            print("""
            1. 查询
            2. 捐书
            3. 借书
            4. 还书
            5. 退出
            """)
            user_input = input('请选择功能对应的数字：')
            if user_input.isdigit() == False:
                print('内容有除数字以外的信息，请重新输入！')
                continue
            choice = int(user_input)
            if choice == 1:
                self.show_books()
            elif choice == 2:
                self.add_book()
            elif choice == 3:
                self.borrow_book()
            elif choice == 4:
                self.return_books()
            elif choice == 5:
                print('Thank!')
                break
            else:
                print('ERROE!请重新输入！！！')
    def add_book(self):
        # 添加书籍
        new_name = input('请输入书籍名称：')
        new_author = input('请输入书籍作者：')
        new_info = input('请输入书籍简介：')
        if self.check_book(new_name):
            self.books[new_name][2] += 1
        else:
            new_book = Book(new_name, new_author, new_info)
            self.books.update(new_book.book_to_dict())
        print('saved!')
    def show_books(self):
        # 显示所有书籍
        if len(self.books) == 0:
            print('图书馆刚成立，还没有图书呢！')
            return
        for key, value in self.books.items():
            book = Book(key, value[0], value[1], value[2])
            print(book)
    def borrow_book(self):
        # 借阅书籍
        borrow_name = input('请输入借阅图书名称：')
        res = self.check_book(borrow_name)
        if res == True:
            if self.books[borrow_name][2] == 0:
                print('你来晚了，都借阅出去了！')
            else:
                self.books[borrow_name][2] -= 1
                print('借阅成功！')
        else:
            print('图书馆目前没有这本书呢！')
    def return_books(self):
        # 还书
        name = input('请输入归还书籍名称：')
        res = self.check_book(name)
        if res:
            if res.status == 1:
                res.status = 0
                print('归还成功，欢迎再次借阅！')
            else:
                print('还未借阅出去！')
        else:
            print('请确认图书名称！')
    def check_book(self, name):
        # 判断书籍是否存在：如果存在，返回书籍，否则返回None
        return name in self.books
    def save_file(self):
        with open('./book_storage.json', 'w') as f:
            json.dump(self.books, f, ensure_ascii=False, indent=4)

if __name__ == '__main__':
    bm = BookManager()
    bm.menu()
    bm.save_file()